<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2015/9/17
 * Time: 14:22
 */
class MenuModel extends Model{
    /**
     * 获取菜单
     * @param $mpid
     * @return array
     */
    public function getIndexMenu()
    {
        $menu = array();
        $map = array();
        $map['fid'] = 0;
        $menu = $this->where($map)->order('orders ASC')->select();
        $keyModel=D('KeyWord');
        $newModel=D('News');
        foreach ($menu as $key => $m) {
            $map['fid'] = $m['itemid'];
            $info= $this->where($map)->order('orders ASC')->select();
            foreach($info as $k=>$v){
                if($info[$k]['type']=='click'){
                    $keyInfo=$keyModel->where(array('keyword'=>$info[$k]['key']))->find();
                    if($keyInfo['type']=='text'){
                        $info[$k]['key']='<h4 style="color:blue">触发关键字：'. $info[$k]['key'].'，回复文字：'.$keyInfo['value'].'</h4>';
                    }
                    elseif($keyInfo['type']=='new'){
                        $x=explode(',',$keyInfo['value']);
                        $temp='';
                        foreach($x as $y=>$z){
                            if(empty($temp)){
                                $temp.=$newModel->where(array('id'=>$z))->getField('title');
                            }else{
                                $temp.='、'.$newModel->where(array('id'=>$z))->getField('title');
                            }
                        }
                        $info[$k]['key']='<h4 style="color:red">触发关键字：'. $info[$k]['key'].'，回复图文：'.$temp.'</h4>';
                    }

                }
                if($info[$k]['type']=='view'){
                    $info[$k]['url']='访问网页：'.$info[$k]['url'];
                }
            }
            $menu[$key]['sub_button'] =$info;
        }
        return $menu;
    }
    /**
     * 获取菜单
     * @param $mpid
     * @return array
     */
    public function getMenu()
    {
        $menu = array();
        $map = array();
        $map['fid'] = 0;
        $menu = $this->where($map)->order('orders ASC')->select();
        foreach ($menu as $key => $m) {
            $map['fid'] = $m['itemid'];
            $info= $this->where($map)->order('orders ASC')->select();
            $menu[$key]['sub_button'] =$info;
        }
        return $menu;
    }

    /**
     * 返回制定等级菜单数量
     * @param $mpid
     * @param int $fid
     * @return int
     */
    public function getLevelCount($fid = 0)
    {
        $map = array();
        $map['fid'] = intval($fid);
        return $this->getWhereCount($map);
    }

    public function getWhereCount($where)
    {
        return $this->where($where)->count();
    }
    /**
     * 获取一定等级项
     * @param $mpid
     * @param int $fid
     * @return mixed
     */
    public function getLevelItem($fid = 0)
    {
        $map = array();
        $map['fid'] = intval($fid);
        return $this->where($map)->order('orders ASC')->select();
    }
}